package com.webchat.pgc.service;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.enums.RedisKeyEnum;
import com.webchat.common.service.RedisService;
import com.webchat.common.service.SnowflakeIdGeneratorService;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.vo.request.MessageCardTemplateRequestVO;
import com.webchat.domain.vo.response.MessageCardTemplateResponseVO;
import com.webchat.pgc.repository.dao.IMessageCardTemplateDAO;
import com.webchat.pgc.repository.entity.MessageCardTemplateEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class MessageCardTemplateService {

    @Autowired
    private IMessageCardTemplateDAO messageCardTemplateDAO;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SnowflakeIdGeneratorService snowflakeIdGeneratorService;


    /**
     * 创建或更新服务号消息卡片模版配置
     *
     * @param messageCardTemplateRequest
     * @return
     */
    public String save(MessageCardTemplateRequestVO messageCardTemplateRequest) {

        MessageCardTemplateEntity entity = this.convert(messageCardTemplateRequest);
        messageCardTemplateDAO.save(entity);
        /**
         * 刷新模版配置缓存
         */
        this.refreshMessageCardTemplateCache(entity);
        return entity.getTemplateId();
    }

    /**
     * 根据模版id查询模版详情配置
     *
     * @param templateId
     * @return
     */
    public MessageCardTemplateResponseVO get(String templateId) {
        String cacheKey = RedisKeyEnum.MESSAGE_CARD_TEMPLATE_CACHE.getKey(templateId);
        String cache = redisService.get(cacheKey);
        if (StringUtils.isNotBlank(cache)) {
            return JsonUtil.fromJson(cache, MessageCardTemplateResponseVO.class);
        }
        return this.refreshMessageCardTemplateCache(templateId);
    }

    /**
     * 模版列表查询
     *
     * @return
     */
    public APIPageResponseBean<MessageCardTemplateResponseVO> page() {

        return null;
    }


    /**
     * 刷新模版配置缓存
     *
     * @param templateId
     */
    private MessageCardTemplateResponseVO refreshMessageCardTemplateCache(String templateId) {
        MessageCardTemplateEntity entity = messageCardTemplateDAO.findByTemplateId(templateId);
        return this.refreshMessageCardTemplateCache(entity);
    }

    private MessageCardTemplateResponseVO refreshMessageCardTemplateCache(MessageCardTemplateEntity entity) {
        MessageCardTemplateResponseVO messageCardTemplate = this.convert(entity);
        if (messageCardTemplate == null) {
            return null;
        }
        String cacheKey = RedisKeyEnum.MESSAGE_CARD_TEMPLATE_CACHE.getKey(entity.getTemplateId());
        redisService.set(cacheKey, JsonUtil.toJsonString(messageCardTemplate),
                RedisKeyEnum.MESSAGE_CARD_TEMPLATE_CACHE.getExpireTime());
        return messageCardTemplate;
    }

    private MessageCardTemplateEntity convert(MessageCardTemplateRequestVO requestVO) {
        String templateId = requestVO.getTemplateId();
        MessageCardTemplateEntity messageCardTemplate;
        if (StringUtils.isNotBlank(templateId)) {
            messageCardTemplate = messageCardTemplateDAO.findByTemplateId(templateId);
            Assert.notNull(messageCardTemplate, "模版更新失败：模版不存在！");
        } else {
            messageCardTemplate = new MessageCardTemplateEntity();
            templateId = snowflakeIdGeneratorService.generateId();
            messageCardTemplate.setTemplateId(templateId);
            messageCardTemplate.setAccount(requestVO.getAccount());
            messageCardTemplate.setCreateBy(requestVO.getOperator());
        }
        messageCardTemplate.setLogo(requestVO.getLogo());
        messageCardTemplate.setTitle(requestVO.getTitle());
        messageCardTemplate.setContent(requestVO.getContent());
        messageCardTemplate.setRedirectName(requestVO.getRedirectName());
        messageCardTemplate.setRedirectUrl(requestVO.getRedirectUrl());
        messageCardTemplate.setUpdateBy(requestVO.getOperator());
        return messageCardTemplate;
    }

    private MessageCardTemplateResponseVO convert(MessageCardTemplateEntity entity) {
        if (entity == null) {
            return null;
        }
        MessageCardTemplateResponseVO messageCardTemplate = new MessageCardTemplateResponseVO();
        BeanUtils.copyProperties(entity, messageCardTemplate);
        Date updateDate = entity.getUpdateDate();
        updateDate = updateDate == null ? entity.getCreateDate() : updateDate;
        messageCardTemplate.setUpdateTime(updateDate.getTime());
        return messageCardTemplate;
    }
}
